<?php 
namespace app\index\controller;
use app\index\controller\Common;
use think\Request;
use think\Db;

class Cases extends Common{
    public function index(){
        $ind_sub = Db::name('nav')->where('sid', 3)->select();
        $banner = Db::name('banner')->field('img_url,link_url')->where('is_show', 1)->limit(1)->find();
        $banner = empty($banner) ? null : $banner;

        $case_type = Request::instance()->param('caseType');
        if(empty($case_type)){
            $cases = Db::name('case')->paginate(6);
        }else{
            $cases = Db::name('case')->where('ctid', $case_type)->paginate(6);
        }
        $case_arr = [];
        foreach($cases as $key => $per){
            $case_arr[$key]['cid'] = $per['cid'];
            $case_arr[$key]['title'] = mb_substr($per['title'], 0, 15);
            $case_arr[$key]['content'] = mb_substr($per['content'], 0, 20);
            $case_arr[$key]['img_url'] = $per['img_url'];
        }

        $page = $cases->render();
        $company = [
            'sub' => $ind_sub,
            'banner' => $banner,
            'cases' => $case_arr,
            'page' => $page,
        ];
        $this->assign('cases', $company);
        return $this->fetch('cases');
    }

    public function caseDetail($cid){
        $banner = Db::name('banner')->where('is_show', 1)->field('img_url,link_url')->limit(1)->find();
        $banner = empty($banner) ? null : $banner;
        $ind_sub = Db::name('nav')->where('sid', 3)->select();

        $case = Db::name('case')->where('cid', $cid)->field('cid,content,title,img_url')->find();
        $prev = Db::name('case')->where('cid', '<', $cid)->field('cid,title')->limit(1)->find();
        $next = Db::name('case')->where('cid', '>', $cid)->field('cid,title')->limit(1)->find();

        if($prev == null){
            $prev = '';
        }else{
            $prev['title'] = mb_substr($prev['title'],0,4);
        }
        if($next == null){
            $next = '';
        }else{
            $next['title'] = mb_substr($next['title'],0,4);
        }

        $company = [
            'sub' => $ind_sub,
            'banner' => $banner,
            'case' => $case,
            'prev' => $prev,
            'next' => $next,
        ];
        $this->assign('detail', $company);
        return $this->fetch('case_detail');
    }
}